import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/phoenix/common/widget/dk_underline_tabindicator.dart';
import 'package:dk_flutter_module_sale/phoenix/information/api/information_api.dart';
import 'package:dk_flutter_module_sale/phoenix/information/bean/bean_information_category.dart';
import 'package:dk_flutter_module_sale/phoenix/information/page/page_information_list.dart';
import 'package:flutter/material.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/user_track/user_track.dart';
import 'package:dk_flutter_module_sale/phoenix/information/bean/ut_informaiton.dart';

class InformationPage extends CommonPage {
  @override
  void onInitState(CommonPageBloc pageBloc) {
    pageBloc.pageHttpGet(
      path: InformationApi.informationCategory,
      widgetBuilderByData: (dynamic data) {
        BeanInformationCategory list = BeanInformationCategory.fromJson(data)??BeanInformationCategory();
        return InformationCategory(list);
      },
      handleErrorCode: (responseData) {
        showToast(responseData.msg);
      },
      showFailMsg: true,
    );
    UserTrack.trackViewScreen(InformationUt.informationPage, null);
  }
}

class InformationCategory extends StatefulWidget {
  final BeanInformationCategory category;

  InformationCategory(this.category);

  @override
  State<StatefulWidget> createState() => new InformationCategoryState();
}

class InformationCategoryState extends State<InformationCategory>
    with SingleTickerProviderStateMixin {
  TabController controller;
  List<Widget> tabList;
  List<Widget> viewList;

  @override
  void initState() {
    super.initState();
    // 类别数量
    int size = widget.category.list.length;
    controller = TabController(length: size, vsync: this);
    tabList = List();
    viewList = List();
    //构造tabs
    for (int i = 0; i < size; i++) {
      tabList.add(Tab(text: widget.category.list[i].categoryName));
      viewList.add(InformationListPage(widget.category.list[i].categoryId));
    }

    controller.addListener((){
      if (controller.index.toDouble() == controller.animation.value){
        UserTrack.track(InformationUt.informationClickCategory, {"name":widget.category.list[controller.index].categoryName});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(49),
        child: AppBar(
          backgroundColor: Colors.white,
          title: Container(
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            child: TabBar(
                isScrollable: true,
                controller: controller,
                unselectedLabelColor: Color.fromARGB(0xff, 0xad, 0xb4, 0xc2),
                labelColor: Color.fromARGB(0xff, 0x32, 0x74, 0xf9),
                labelStyle: TextStyle(fontSize: 17,fontWeight: FontWeight.bold),
                labelPadding: EdgeInsets.only(left:8,right: 8),
                indicator: DKUnderlineTabIndicator(
                  insets: const EdgeInsets.only(bottom:5),
                  wantWidth: 20,
                  borderSide: BorderSide(
                    width: 2,
                    color: Color.fromARGB(0xff, 0x32, 0x74, 0xf9),
                  ),
                ),
                tabs: tabList),
          ),
          automaticallyImplyLeading: false,
        ),
      ),
      body: new TabBarView(
        children: viewList,
        controller: controller,
      ),
    );
  }
}
